package com.sq.universe.base.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import  com.sq.universe.base.controller.BaseController;
import  com.sq.universe.base.entity.RespEntity;
import  com.sq.universe.base.entity.LogEntity;
import  com.sq.universe.base.service.LogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/base/log")
@CrossOrigin
public class LogController extends BaseController {

    @Autowired
    private LogService logService;

    /**
    * 查询数据
    * @param logEntity
    * @return
    */
    @PostMapping("/query")
    @RequiresPermissions("base:log:query")
    public RespEntity query(LogEntity logEntity){
        try {
            return new RespEntity("查询成功",logService.query(logEntity));
        }catch (Exception exception){
            return new RespEntity("查询失败");
        }
    }

    /**
    * 分页查询
    * @param logEntity
    * @return
    */
    @PostMapping("/queryByPage")
    @RequiresPermissions("base:log:queryByPage")

    public RespEntity queryByPage(LogEntity logEntity){
        try {
            PageHelper.startPage(logEntity.getPageNo(),logEntity.getPageSize());
            List<LogEntity> logList = logService.query(logEntity);
            PageInfo<LogEntity> pageInfo = new PageInfo<LogEntity>(logList);
            return new RespEntity("查询成功",pageInfo);
        }catch (Exception exception){
            return new RespEntity("查询失败");
        }
    }

    /**
    * 插入
    * @param logEntity
    * @return
    */
    @PostMapping("/insert")
    @RequiresPermissions("base:log:insert")
    public RespEntity insert(LogEntity logEntity){
        try {
            logService.insert(logEntity);
            return new RespEntity("保存成功");
        }catch (Exception exception){
            return new RespEntity("保存失败");
        }
    }

    /**
    * 更新
    * @param logEntity
    * @return
    */
    @PostMapping("/update")
    @RequiresPermissions("base:log:update")
    public RespEntity update(LogEntity logEntity){
        try {
            logService.update(logEntity);
            return new RespEntity("更新成功");
        }catch (Exception exception){
            return new RespEntity("更新失败");
        }
    }

    /**
    * 删除成功
    * @param logEntity
    * @return
    */
    @PostMapping("/delete")
    @RequiresPermissions("base:log:delete")
    public RespEntity delete(LogEntity logEntity){
        try {
            if(logEntity.getLogId() == null){
                return new RespEntity("删除失败");
            }
            logService.logicalDelete(logEntity);
            return new RespEntity("删除成功");
        }catch (Exception exception){
            return new RespEntity("删除失败");
        }
    }
}